export class ArrayUtil {
    public static add(list: any[], item: any) {
        if (list.indexOf(item) == -1) {
            list.push(item);
            return true;
        }
        return false;
    }

    public static copy(list) {
        let result = [];
        for (let i = 0; i < list.length; i++) {
            result.push(list[i]);
        }
        return result;
    }


    public static random(list: any[]) {
        return list[Math.floor(Math.random() * list.length)];
    }

    public static randomCount(list: any[], count, target = []) {
        if (count == 0) {
            return target;
        } else {
            let radnom = ArrayUtil.random(list);
            target.push(radnom);
            list.splice(list.indexOf(radnom), 1);
            return ArrayUtil.randomCount(list, count - 1, target);
        }
    }

    public static remove(list: any[], item: any) {
        let index = list.indexOf(item);
        if (index != -1) {
            list.splice(index, 1);
            return true;
        }
        return false;
    }


    //去重
    public static unique(list: any[]) {
        let result = [];
        for (let i = 0; i < list.length; i++) {
            if (result.indexOf(list[i]) == -1) {
                result.push(list[i]);
            }
        }
        return result;
    }
    

}